package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeRemoteExpertLoginService;
import com.bidevalution.core.util.WebServiceUtils;
import com.bidevalution.dao.mapper.BeSectionExpertInfoMapper;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.exception.BizException;
import com.qtp.core.util.DesUtil;
import com.google.common.collect.Maps;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.Md5Util;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Map;

/**
 * Created by glodon on 2019-12-27.
 */
@Log4j2
@Service
public class BeRemoteExpertLoginServiceImpl implements BeRemoteExpertLoginService {

    @Value("${WEBSERVICE_URL_GT}")
    private String WEBSERVICE_URL_GT;

    @Value("${TOKEN_LOGIN_SECRET}")
    private String TOKEN_LOGIN_SECRET;

    private static final String EXPERT_INFO_METHODS = "getExpertInfo";

    @Autowired
    private BeSectionExpertInfoMapper beSectionExpertInfoMapper;
    /**
     * 获取专家验证token
     * @param loginId
     * @return
     * @throws Exception
     */
    @Override
    public String getExpertToken(String loginId) throws Exception {
        //调用协调系统接口获取专家信息
        //生成MD5值 ，生成规则：“hz”+系统时间（yyyy-mm-dd）+”pbsys”的MD5值
        String MD5 = Md5Util.getMD5("hz"+ DateUtil.getCurrentDateStr(DateUtil.PT1) + "pbsys");
        log.info("======调用国泰协调系统webservice地址："+WEBSERVICE_URL_GT+"======");
        log.info("======调用参数：loginid="+ loginId +", MD5=" +MD5 +"======");
        //测试数据，待联调
        //String result = "{\"success\":\"1\",\"expert_name\":\"sh01\",\"expert_speciality\":\"测试专业\",\"expert_code\":\"130182199202154845\",\"projectno\":\"01121120191203036\",\"pingbiaodate\":\"2019-12-11 14:00:00\"}";
        String result = WebServiceUtils.callWebSV(WEBSERVICE_URL_GT, EXPERT_INFO_METHODS, loginId, MD5);
        log.info("======返回结果："+result +"======");
        if(StringUtils.isNotBlank(result)) {
            Map<String, Object> map = GsonUtil.jsonToMapWithGson(result);
            if (map != null) {
                if(Constants.ONE.equals(OConvertUtils.getInt(map.get("success")))){
                    //评委身份证号
                    String expertCode = OConvertUtils.getString(map.get("expert_code"));
                    //标段编号
                    String projectNo = OConvertUtils.getString(map.get("projectno"));
                    String loginName = getExpertLoginName(projectNo,expertCode);
                    log.info("查询到专家姓名：" + loginName);
                    if (StringUtils.isBlank(loginName)) {
                        throw new BizException("评标系统中未查询到专家录入信息！");
                    } else {
                        Map<String, Object> resultMap = Maps.newHashMap();
                        resultMap.put("loginTime",DateUtil.getCurrentTime());
                        resultMap.put("loginName", loginName);
                        //对称加密json(登录名，时间戳)字符串
                        return DesUtil.encrypt(GsonUtil.toJson(resultMap), TOKEN_LOGIN_SECRET);
                    }
                } else {
                    throw new BizException("协调系统接口调用失败："+OConvertUtils.getString(map.get("errormsg")));
                }
            } else {
                throw new BizException("协调系统接口调用失败！");
            }
        }else {
            throw new BizException("协调系统接口调用失败！");
        }
    }

    /**
     * 根据专家身份证号和标段编号查询专家登录名
     * @param sectionCode
     * @param expertCardNo
     * @return
     * @throws Exception
     */
    private String getExpertLoginName(String sectionCode, String expertCardNo) throws Exception {
        return beSectionExpertInfoMapper.getExpertLoginName(sectionCode, expertCardNo);
    }
}
